package com.awspaas.user.apps.publicservice.util;

import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;

import static com.awspaas.user.apps.publicservice.util.HttpClientUtil.sendPost;


/**
 * @author:Lizj
 * @create: 2022-10-09 23:02
 * @Description: 发送消息
 */
public class SendWechatMsgUtil {

    /**
     * @methodName: 获取access_token
     * @param:
     * @return:
     * @description:
     * @auther: Lizj
     * @date: 2022/10/9 23:16
     */
    public String getAccessToken(
            String corpid,        //企业ID
            String corpsecret     //应用凭证密钥
    ){
        String wxAccessToken="";
        //获取access_token
        String uri= SDK.getAppAPI().getProperty("com.awspaas.user.apps.public.service","wxAccessToken");
        String para="corpid="+corpid+"&corpsecret="+corpsecret;
        wxAccessToken= HttpRequestUtil.sendGet(uri,para);
        JSONObject wxAccessTokenJO = JSONObject.parseObject(wxAccessToken);
        SDK.getLogAPI().consoleInfo("获取凭证接口"+wxAccessTokenJO);
        String accessToken = wxAccessTokenJO.getString("access_token");
        return accessToken;
    }

    /**
     * @methodName: 发送应用消息
     * @param:
     * @return: 
     * @description:
     * @auther: Lizj
     * @date: 2022/10/9 23:32
     */
    public JSONObject sendAppMsg(
            String accessToken, //调用接口凭证
            JSONObject para //消息体
    ){
        String result="";
        //发送待办信息到企业微信
        String uri= SDK.getAppAPI().getProperty("com.awspaas.user.apps.public.service","wxSendAppMsg")+"?access_token="+accessToken;
       // SDK.getLogAPI().consoleInfo("发送消息："+uri);

        result= HttpRequestUtil.sendPost(uri,para.toString(),false);
        SDK.getLogAPI().consoleInfo("发送企业微信消息"+result);
        Object result2=JSONObject.parse(result);
        return (JSONObject) result2;
    }

    /**
     * 企业微信系统消息撤回功能
     * @param taskinstId
     * @return
     */
    public static String reCall(String taskinstId,String accessToken){
        String result = "";

        try {
            String wechatmsgId = DBSql.getString("SELECT WECHATMSGID FROM BO_EU_O45_TASK_SYNC_LOG " +
                    "WHERE TASKINST_ID =?",new Object[]{taskinstId});
            if(wechatmsgId == "" | wechatmsgId.equals("")){
                System.out.println("撤回失败，任务实例ID：" + taskinstId + "不存在。");
            }
            //获取access_token
//            WechatModel wechatModel = WechatCache.getModelByCorpId(corpId);
//            WechatAppsModel appsModel = WechatAppsCache.loadWechatApp(wechatModel.getId(), agentId);
//            AccessToken token = AccessTokenCache.getToken(appsModel);
//            String accessToken = token.getAccessToken();
            System.out.println("accessToken-----------"+accessToken);

            //接口地址
            String url = "https://qyapi.weixin.qq.com/cgi-bin/message/recall?access_token="+accessToken;

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msgid",wechatmsgId);

            result = sendPost(url,jsonObject);


        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

}
